package com.bdd.service.Impl;


import com.bdd.entity.Inventory;
import com.bdd.entity.OrderItem;
import com.bdd.entity.Return;
import com.bdd.entity.ReturnItem;
import com.bdd.mapper.*;
import com.bdd.model.bo.ReturnBO;
import com.bdd.model.bo.ReturnItemBO;
import com.bdd.model.bo.order.OrderItemListBO;
import com.bdd.service.ReturnService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

@Service
public class ReturnServiceImpl implements ReturnService {

    @Autowired
    DiscountDao discountDao;

    @Autowired
    ClothingDao clothingDao;

    @Autowired
    SizeDao sizeDao;

    @Autowired
    InventoryDao inventoryDao;

    @Autowired
    OrderItemDao orderItemDao;

    @Autowired
    ReturnDao returnDao;

    @Autowired
    ReturnItemDao returnItemDao;
    private CopyOnWriteArrayList<ReturnItemBO> returnItemList = new CopyOnWriteArrayList<>();

    @Override
    public Page<ReturnBO> list(Integer returnId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<ReturnBO> returnBOList = returnDao.selectAllByReturnId(returnId);
        return returnBOList;
    }

    @Override
    public void returnItem(Integer orderItemId, String returnReason) {
//        添加缓存
        OrderItem orderItem = orderItemDao.selectByOrderItemId(orderItemId);
        orderItem.setStatus("RETURNED");
        orderItemDao.updateByPrimaryKey(orderItem);

        OrderItemListBO orderItemListBO = orderItemDao.selectBOByOrderItemId(orderItemId);
        ReturnItemBO returnItemBO = ReturnItemBO.builder()
                .orderItemId(orderItemId)
                .newPrice(orderItemListBO.getNewPrice())
                .count(orderItemListBO.getCount())
                .clothingId(orderItemListBO.getId())
                .clothingName(orderItemListBO.getName())
                .oldPrice(orderItemListBO.getOldPrice() * orderItemListBO.getCount())
                .sizeId(orderItemListBO.getSizeId())
                .size(orderItemListBO.getSize())
                .orderId(orderItemListBO.getOrderItemOrderId())
                .discount(orderItemListBO.getDiscount())
                .status(orderItemListBO.getStatus())
                .returnReason(returnReason)
                .build();
        returnItemList.add(returnItemBO);

    }

    @Override
    public Map<String, Object> cacheList() {
        List<ReturnItemBO> returnItemBOList = this.returnItemList;
        Map<String, Object> map = new HashMap<>();
        double totalPrice = 0d;
        int totalCount = 0;
        for (ReturnItemBO returnItemBO : returnItemList) {
            totalPrice += returnItemBO.getNewPrice();
            totalCount += returnItemBO.getCount();
        }
        map.put("returnItemBOList", returnItemBOList);
        map.put("totalPrice", totalPrice);
        map.put("totalCount", totalCount);
        return map;
    }

    @Override
    public Return refund(Integer userId) {
        if (returnItemList == null){
            throw new IllegalArgumentException("退货列表不可为空");
        }
//        插入退货表
        Map<String,Object> map = new HashMap<>();
        double totalPrice = 0d;
        int totalCount = 0;
        Integer orderId = null;
        Date date = null;
        for (ReturnItemBO returnItemBO : returnItemList) {
            totalPrice += returnItemBO.getNewPrice();
            totalCount += returnItemBO.getCount();
            orderId = returnItemBO.getOrderId();
        }
        Return returnReturn = Return.builder()
                .totalPrice(totalPrice)
                .totalCount(totalCount)
                .orderId(orderId)
//                .returnTime(new Date())
                .build();
        returnDao.insertSelective(returnReturn);

        for (ReturnItemBO returnItemBO : returnItemList){
//        插入退货项
            ReturnItem returnItem = ReturnItem.builder()
                    .returnId(returnReturn.getId())
                    .orderItemId(returnItemBO.getOrderItemId())
                    .orderId(returnItemBO.getOrderId())
                    .price(returnItemBO.getNewPrice())
                    .returnReason(returnItemBO.getReturnReason())
                    .build();
            returnItemDao.insertSelective(returnItem);
//            更新订单状态
            OrderItem orderItem = orderItemDao.selectByOrderItemId(returnItem.getOrderItemId());
            orderItem.setStatus("REFUNDED");
            orderItemDao.updateByPrimaryKey(orderItem);

//            更新库存
            Inventory inventory = inventoryDao.selectByCothingId(orderItem.getClothingId());
            inventory.setQuantity(inventory.getQuantity() + returnItemBO.getCount());
            inventoryDao.updateByPrimaryKey(inventory);
        }
        returnItemList.clear();
        return returnDao.selectByPrimaryKey(returnReturn.getId());
    }

    public void cancel(Integer userId){
//        取消退款
        for (ReturnItemBO returnItemBO : returnItemList){
            OrderItem orderItem = orderItemDao.selectByOrderItemId(returnItemBO.getOrderItemId());
            orderItem.setStatus("PAID");
            orderItemDao.updateByPrimaryKey(orderItem);
        }
        returnItemList.clear();
    }

}
